const Koa = require('koa');
const app = new Koa();
const bodyParser = require('koa-bodyparser');
const multy = require('multy');
//corss request
app.use(require('koa2-cors')({credentials:true}));

app.use(bodyParser());
app.use(multy());

//appToken auth
const appTokenAuth = require('./providers/appTokenAuth');
app.use(appTokenAuth);

//jwtToken Auth
const jwtTokenAuth = require('./providers/jwtTokenAuth');
jwtTokenAuth.authConfig(app);

//service Routes
const appRouter = require('koa-router')();
const serviceRoutes = require('./services');
serviceRoutes.attachRoutes(app,appRouter);
app.use(appRouter.routes())
   .use(appRouter.allowedMethods());

const apiStatus = require("./apiStatus");
app.use(ctx => {
    if (ctx.apiBody){
        ctx.body = {
            apiStatus: apiStatus.success,
            result: ctx.apiBody
        }
    }else if (ctx.apiStatus){
        ctx.body = {
            apiStatus: ctx.apiStatus
        }
    }else if (!ctx.body){
        ctx.body = {
      		apiStatus: apiStatus.requestInvalid
      	};
    }
});

var appConfig = require('./configs/appConfig');
app.listen(appConfig.webServer.port.mbsApi);
